package com.xuelang.pipeline.infrastructure.pipeline.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.pipeline.dto.PipelineGroupQuery;
import com.xuelang.pipeline.domain.pipeline.PipelineGroup;
import com.xuelang.pipeline.domain.pipeline.repository.PipelineGroupRepository;
import com.xuelang.pipeline.infrastructure.pipeline.converter.PipelineGroupToPipelineGroupDOConverter;
import com.xuelang.pipeline.infrastructure.pipeline.dataobject.PipelineGroupDO;
import com.xuelang.pipeline.infrastructure.pipeline.mapper.PipelineGroupMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 流水线分组仓库实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Slf4j
@Repository
public class PipelineGroupRepositoryImpl implements PipelineGroupRepository {

    @Autowired
    PipelineGroupMapper pipelineGroupMapper;

    @Override
    public PageRecord<PipelineGroup> pageList(long currentPage, long pageSize, PipelineGroup pipelineGroup) {
        currentPage = currentPage < 1 ? 1 : currentPage;
        pageSize = pageSize < 1 ? 10 : pageSize;
        Page<PipelineGroupDO> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<PipelineGroupDO> wrapper = Wrappers.lambdaQuery(PipelineGroupDO.class);
        if (pipelineGroup != null) {
            if (StringUtils.isNotBlank(pipelineGroup.getPipelineCode())) {
                wrapper.eq(PipelineGroupDO::getPipelineCode, pipelineGroup.getPipelineCode());
            }
        }
        pipelineGroupMapper.selectPage(page, wrapper);
        return new PageRecord<>(
                page.getCurrent(),
                page.getSize(),
                page.getTotal(),
                page.getPages(),
                page.getRecords().stream().map(PipelineGroupToPipelineGroupDOConverter.INSTANCE::pipelineGroupDOToPipelineGroup).collect(Collectors.toList())
        );
    }

    @Override
    public List<PipelineGroup> list(PipelineGroup pipelineGroup) {
        LambdaQueryWrapper<PipelineGroupDO> wrapper = Wrappers.lambdaQuery(PipelineGroupDO.class);
        if (pipelineGroup != null) {
            if (StringUtils.isNotBlank(pipelineGroup.getPipelineCode())) {
                wrapper.eq(PipelineGroupDO::getPipelineCode, pipelineGroup.getPipelineCode());
            }
        }
        return pipelineGroupMapper.selectList(wrapper).stream()
                .map(PipelineGroupToPipelineGroupDOConverter.INSTANCE::pipelineGroupDOToPipelineGroup).collect(Collectors.toList());
    }


    @Override
    public Boolean save(PipelineGroup pipelineGroup) {
        return pipelineGroupMapper.insert(PipelineGroupToPipelineGroupDOConverter.INSTANCE.pipelineGroupToPipelineGroupDO(pipelineGroup)) == 1;
    }

    @Override
    public Boolean update(PipelineGroup pipelineGroup) {
        return pipelineGroupMapper.update(
                PipelineGroupToPipelineGroupDOConverter.INSTANCE.pipelineGroupToPipelineGroupDO(pipelineGroup),
                Wrappers.lambdaQuery(PipelineGroupDO.class).eq(PipelineGroupDO::getId, pipelineGroup.getId())) == 1;
    }

    @Override
    public Boolean delete(String uuid) {
        return pipelineGroupMapper.delete(Wrappers.lambdaQuery(PipelineGroupDO.class).eq(PipelineGroupDO::getId, uuid)) == 1;
    }

    @Override
    public Boolean deleteByCondition(PipelineGroup condition) {
        LambdaQueryWrapper<PipelineGroupDO> wrapper = Wrappers.lambdaQuery(PipelineGroupDO.class);
        Long id = condition.getId();
        String creatorId = condition.getCreatorId();
        String groupUuid = condition.getGroupUuid();
        String pipelineCode = condition.getPipelineCode();
        wrapper.eq(Objects.nonNull(id), PipelineGroupDO::getId, id);
        wrapper.eq(StringUtils.isNotBlank(creatorId), PipelineGroupDO::getCreatorId, creatorId);
        wrapper.eq(StringUtils.isNotBlank(groupUuid), PipelineGroupDO::getGroupUuid, groupUuid);
        wrapper.eq(StringUtils.isNotBlank(pipelineCode), PipelineGroupDO::getPipelineCode, pipelineCode);

        return pipelineGroupMapper.delete(wrapper) == 1;
    }

    @Override
    public PipelineGroup query(String uuid) {
        PipelineGroupDO pipelineGroupDO = pipelineGroupMapper.selectOne(Wrappers.lambdaQuery(PipelineGroupDO.class).eq(PipelineGroupDO::getId, uuid));
        return PipelineGroupToPipelineGroupDOConverter.INSTANCE.pipelineGroupDOToPipelineGroup(pipelineGroupDO);
    }

    @Override
    public List<PipelineGroup> queryByGroupUuid(String groupUuid) {
        List<PipelineGroupDO> pipelineGroupDOList = pipelineGroupMapper.selectList(Wrappers.lambdaQuery(PipelineGroupDO.class)
                .eq(PipelineGroupDO::getGroupUuid, groupUuid));
        return pipelineGroupDOList.stream().map(PipelineGroupToPipelineGroupDOConverter.INSTANCE::pipelineGroupDOToPipelineGroup).collect(Collectors.toList());
    }

    @Override
    public Boolean deleteByPipelineCode(String pipelineCode) {
        pipelineGroupMapper.delete(Wrappers.lambdaQuery(PipelineGroupDO.class).eq(PipelineGroupDO::getPipelineCode, pipelineCode));
        return true;
    }

    @Override
    public Boolean modifyPipelineGroups(PipelineGroupQuery query, String creatorId) {
        String pipelineCode = query.getPipelineCode();
        List<String> groupUuidList = query.getGroupUuidList();
        // 查询现有关联关系并删除
        LambdaQueryWrapper<PipelineGroupDO> wrapper = Wrappers.lambdaQuery(PipelineGroupDO.class);
        wrapper.eq(PipelineGroupDO::getPipelineCode, pipelineCode);
        // 删除
        pipelineGroupMapper.delete(wrapper);

        // 若分组为空，则直接返回
        if(CollectionUtils.isEmpty(groupUuidList)){
            return true;
        }

        // 整理批量插入的实体
        List<PipelineGroupDO> toAddList = Lists.newArrayList();
        for(String groupUuid : groupUuidList){
            PipelineGroupDO toAdd = new PipelineGroupDO();
            toAdd.setPipelineCode(pipelineCode);
            toAdd.setGroupUuid(groupUuid);
            toAdd.setCreatorId(creatorId);
            toAdd.setCreateTime(new Date());
            toAddList.add(toAdd);
        }

        // 更新关联关系
        return pipelineGroupMapper.insertBatch(toAddList) != 0;
    }
}
